<?php
// vim:set expandtab tabstop=3 shiftwidth=3 softtabstop=3 foldclose=all foldmethod=marker:
/**
 * 项目：   5anet
 * 文件：   class/album/SaveDir.class.php
 *
 * 保存相册目录
 *
 * PHP Version 5
 *
 * @package:   class.album
 * @author:    Mike.G Chinese Name: 黄叶 <hylinux@gmail.com>
 * @license:   http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1 
 * @copyright: www.5anet.com
 * @version:   $Id: $
 * @date:      $Date: $
 */

include_once CLASS_PATH.'main/BaseAction.class.php';

//包含相册的工具表
include_once CLASS_PATH.'album/AlbumUtil.class.php';
include_once CLASS_PATH.'user/UserUtil.class.php';




class SaveDir extends BaseAction {
   /**
    * 数据库的连接
    */
   private $db;


   /**
    * 构造函数
    * @param:  NULL
    * @return: NULL
    * @access: public
    */
   public function __construct() {
      $this->db = $this->getDB();
   }


   /**
    * 保存相册目录
    * @param:  NULL
    * @return: NULL
    * @access: public
    */
   public function run() {

      //能够运行到这一步，说明
      //用户已经登录了。
      $user_name = $_SESSION['user']['name'];   //得到用户的名称
      $user_id = UserUtil::getUserId($this->db, $user_name);

      //判断用户是否已经建立了相册
      if ( !file_exists(ROOT_PATH.'upload/album/'.$user_id) || 
         !is_dir(ROOT_PATH.'upload/album/') ) {
         //建立用户的相册目录
         mkdir(ROOT_PATH.'upload/album/'.$user_id, 0777);
      }

      $dirname = $this->getParameterFromPOST('dirname');

      if ( !$dirname ) {
         $this->AlertAndBack('没有填写目录名称');
         return;
      }


      if ( AlbumUtil::DirhadExists($this->db, $dirname) ) {
         $this->AlertAndBack('目录已经存在');
         return;
      }

      $sql = 'insert into album_dir (user_id, name) values ( ?, ?) ';
      $sth = $this->db->prepare($sql);
      $this->db->Execute($sth, array($user_id, $dirname));

      $this->forward('index.php?module=album&action=showme');

      return;

   }

}


?>
